const { Types } = require("mongoose");
// 校验ObjectId
const validateObjectId = (id) => {
    return Types.ObjectId.isValid(id);
};
// 校验用户名
const validateUserName = (username) => {
    return /^[a-z]+\w{4,12}/.test(username);
};

// 校验名称
const validateTitle = (title) => {
    return title.length >= 4 && title.length <= 20;
};

// 校验内容
const validateContent = (content) => {
    return content.length >= 4 && content.length <= 200;
};

// 校验电子邮箱
const validateEmail = (email) => {
    return /^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/.test(email);
};

// 校验头像
const validateAvatar = (avatar) => {
    if (!avatar) {
        return false;
    }
    const ext = avatar.split(".")[1] ? avatar.split(".")[1] : "";
    return ["png", "jpg", "jpeg", "gif"].includes(ext);
};

module.exports = {
    validateObjectId,
    validateUserName,
    validateEmail,
    validateAvatar,
    validateTitle,
    validateContent,
};
